<?php
/**
 * Created by PhpStorm.
 * User: pengzhizhuang
 * Date: 17/4/18
 * Time: 上午10:38
 */

namespace App\Http\Traits;

trait ChuanglanTrait
{
    //发送短信接口URL
    protected $apiSendSmsUrl = 'http://sms.253.com/msg/send';
    //发送邮件接口URL
    protected $apiSendEmailUrl = 'http://zz.253.com/emailcard/api/send.html';
    //短信余额查询接口URL
    protected $apiBalanceQueryUrl = 'http://sms.253.com/msg/balance';

    private function config($type = 'sms')
    {
        if ($type == 'sms') {
            return [
                'un' => config('saas.ChuanglanSms.API_SMS_ACCOUNT'),
                'pw' => config('saas.ChuanglanSms.API_SMS_PASSWORD'),
            ];
        }
        return [
            'account' => config('saas.ChuanglanSms.API_EMAIL_ACCOUNT'),
            'password' => config('saas.ChuanglanSms.API_EMAIL_PASSWORD'),
        ];
    }


    /**
     * 发送短信
     *
     * @param  $mobile
     * @param  $msg
     * @param  int    $needstatus
     * @return mixed
     */
    public function sendSms($data, $needstatus = 1)
    {
        if (empty($data['phone'])) {
            die;
        }
        $config = $this->config();
        $postArr = array_merge(
            $config, array(
            'msg' => $data['msg'],
            'phone' => $data['phone'],
            'rd' => $needstatus
            )
        );
        $result = $this->curlPost($this->apiSendSmsUrl, $postArr);
        return $result;
    }

    /**
     * 发送邮件
     *
     * @param  $data
     * @return mixed
     */
    public function sendEmail($data)
    {
        if (empty($data['to'])) {
            die;
        }
        $config = $this->config('email');
        $postArr = array_merge(
            $config, array(
            'from' => $data['from'],
            'to' => $data['to'],
            'subject' => $data['subject'],
            'html' => $data['html'],
            'fromName' => $data['fromName'],
            )
        );
        $result = $this->curlPost($this->apiSendEmailUrl, $postArr);
        return $result;
    }

    /**
     * 通过CURL发送HTTP请求
     *
     * @param  string $url        //请求URL
     * @param  array  $postFields //请求参数
     * @return mixed
     */
    private function curlPost($url, $postFields)
    {
        $postFields = http_build_query($postFields);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }

    /**
     * 查询剩余短信条数
     *
     * @return mixed
     */
    public function queryBalance()
    {
        //查询参数
        $postArr = $this->config();
        $result = $this->curlPost($this->apiBalanceQueryUrl, $postArr);
        return $result;
    }

    /**
     * 处理返回值
     */
    public function execResult($result)
    {
        $result = preg_split("/[,\r\n]/", $result);
        return $result;
    }

    //魔术获取
    public function __get($name)
    {
        return $this->$name;
    }

    //魔术设置
    public function __set($name, $value)
    {
        $this->$name = $value;
    }
}
